//
//  WmcShareButton.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/27.
//

import UIKit

class WmcShareButton: UIView {
    // Lifecycle

    convenience init(title: String, image: UIImage, color: UIColor) {
        self.init()
        self.title = title
        self.image = image
        self.color = color
        self.makeUI()
        self.bindEvent()
    }
    
    // Internal

    private func makeUI() {
        addSubview(circleView)
        addSubview(titleLabel)

        circleView.addSubview(imageView)

        circleView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.width.height.equalTo(56)
            make.centerX.equalToSuperview()
        }

        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.center.equalToSuperview()
        }

        titleLabel.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(circleView.snp.bottom).offset(12)
            make.height.equalTo(20)
            make.width.greaterThanOrEqualTo(56)
        }
    }
    
    private func bindEvent() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        self.addGestureRecognizer(tap)
    }

    @objc
    private func tapAction() {
        clickClosure?()
    }

    var clickClosure: (() -> Void)?
    
    // Private

    private var title: String?

    private var image: UIImage?
    
    private var color: UIColor?

    private lazy var circleView: UIView = {
        let view = UIView()
        view.backgroundColor = color
        view.layer.cornerRadius = 28
        view.layer.masksToBounds = true
        return view
    }()

    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.image = image
        return view
    }()

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = title
        label.textAlignment = .center
        label.font = 16.wmc_font
        label.textColor = .wmc_grayH2_323233
        return label
    }()
}
